//<SCRIPT LANGUAGE=javascript>
function RegCookie(key,daysBeforeExp){
  this.days = daysBeforeExp||-1;
  if( typeof(key) == "string" ){
		key = _key(key);
    this.cookieName = key;
    this.xmlDom = new ActiveXObject("Microsoft.XMLDom");
    if( !getCookie(key) )setCookie(key,"<"+key+"/>");
    if( !this.xmlDom.loadXML(getCookie(key)||"<Empty/>") )
			throw new Error(this.xmlDom.parseError.reason+"\n"+this.xmlDom.parseError.srcText);
    this.root = this.xmlDom.documentElement;
  }
  else{
    this.xmlDom = key.nodeType==9?key:key.ownerDocument;
    this.root   = key.nodeType==9?key.documentElement:key;
  }
  this.getKeyEx = function(key){
    var xmlKey = null;
    if( !key )xmlKey = this.root;
    else if( typeof(key) != "string" )xmlKey = key;
    else xmlKey = this.getKey(key,true);
    return xmlKey;
  }
  this.getKey = getKey;
  function getKey(keyName,test){ 
    if( !this.root ){errorAlert(getFunctionName(this),keyName,"Root does not exists."); return null;}
    var nodeKey = this.root.selectSingleNode("//"+keyName); 
    if( !nodeKey ){
      if( test )return null;
      var parent =this,aNodes = keyName.split("/");
      for( var n=0; n < aNodes.length-1; n++ )
        parent = this.getKey(aNodes[n]);
      nodeKey = parent.root.appendChild(this.xmlDom.createElement(aNodes[n]));
    }
    return new RegCookie(nodeKey,this.days);
  }
  this.getValue = getValue;
  function getValue(valueName,keyName){
    try{
    return !keyName?this.root.getAttribute(valueName):this.getKey(keyName).getValue(valueName);
    }catch(e){return null;}
  }
  this.setValue = setValue;
  function setValue(valueName,value,keyName){
    var l_keyName =  isObject(valueName)?value:keyName;
    var l_value   = !isObject(valueName)?value:null;
    try{
      if( !l_keyName )
        setAttribute(this.root,valueName,value);
      else
        this.getKey(l_keyName).setValue(valueName,l_value);
    }catch(e){return null;}
  }
  this.persist = function(){
    var root = this.xmlDom.documentElement;
    var today = new Date();
    today.setDate(today.getDate()+this.days);
    setCookie(root.nodeName,root.xml,today);
  }
  this.unpersist = function(){
    var root = this.xmlDom.documentElement;
    setCookie(root.nodeName,root.xml,"1/1/1999");
  }
  //locals
  function isObject(v){return typeof(v) == "object";}
  function setAttribute(node,name,value){
    return setAttributes(node,isObject(name)?name:eval("o={"+name+":value}"));
  }
  function setAttributes(node,attributes){
  var a;
    for( a in attributes )
      attributes[a]!=null?node.setAttribute(a,attributes[a]):node.removeAttribute(a);
    return node;
  }
  function errorAlert(objectName,params,description){
    alert(objectName+"."+getErrorPath(errorAlert.caller)+"('"+params+"')\n"+description);
  }
  function setCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) +
        ((expires) && !isNaN(new Date(expires)) ? "; expires=" + new Date(expires).toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
    document.cookie = curCookie;
  }
  function getCookie(name) { // use: getCookie("name");
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
    // Local
  }
  function _key(key){return key.replace(/[^\w]/g,"_");}
}
